﻿/*-------------------------------------------------------------------------
* 命名空间: ScreenCapture.Windows/ClipboardService
* 类       名: ClipboardService
* 功      能:  TD
* 时      间:  2023/12/5 14:02:43
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using ScreenCapture.Base.Images;
using ScreenCapture.Base.Services;
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ScreenCapture.Windows
{
    public class ClipboardService : IClipboardService
    {

        /// <summary>
        /// 因为IMessageProvider在ClipboardService的内部,所以在请求ClipboardService对象的时候,依赖注入的框架
        /// 会自动解析器构造函数的依赖项,也就是IMessageProvider接口,会自动调用Services.Get<IMessageProvider>();
        /// 所以说,这里要首先去绑定IMessageProvider接口.依赖注入框架会查找已经注册的IMessageProvider的实现类,并创建一个
        /// 实例传递给ClipboardService的构造函数.
        /// 这是依赖注入的基本原理,即控制反转(Inversion Of Control), 它允许我们将对象的创建和生命周期管理交给依赖注入框架,
        /// 从而减少了代码的耦合度,提高了代码的可测试行和可维护性.
        /// </summary>
        private readonly IMessageProvider _messageProvider;

        public ClipboardService(IMessageProvider MessageProvider)
        {
            _messageProvider = MessageProvider;
        }

        /// <summary>
        /// 粘贴板设置文本
        /// </summary>
        /// <param name="Text"></param>
        public void SetText(string Text)
        {
            if (Text == null)
                return;

            try
            {
                // 使用系统粘贴板将内容复制到粘贴板上
                Clipboard.SetText(Text);
            }
            catch (ExternalException)
            {
                _messageProvider?.ShowError($"Copy to clipboard failed:\n\n{Text}");
            }
        }
        /// <summary>
        /// 获取文本
        /// </summary>
        /// <returns></returns>
        public string GetText() => Clipboard.GetText();

        /// <summary>
        /// 是否包含文本
        /// </summary>
        public bool HasText => Clipboard.ContainsText();


        /// <summary>
        /// 设置图像
        /// </summary>
        /// <param name="Image"></param>
        public void SetImage(IBitmapImage Image)
        {

        }

        public bool HasImage => throw new NotImplementedException();

        public IBitmapImage GetImage()
        {
            throw new NotImplementedException();
        }

    }
}
